﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SchemaMonitor.json
{
	public class CSoftPhoto : CSoft
	{
		public CSoftPhoto(CNodeBasic root, CUser cache) : base(root, cache)
		{
		}

		public bool IsShare { get { return Type == "share"; } }	//url, title, and media/image

		private bool HasMedia { get { return Fields.Has("media"); } }
		private CNodeBasic Media { get { return Fields["media"].AsComposite; } }
		private bool HasImage { get { return HasMedia && Media.Fields.Has("image"); } }
		private CNodeBasic Image { get { return Media.Fields["image"].AsComposite; } }


		public string ImageUrl { get { return HasImage ? Image.GetString("url") : null; } }
		public string Url { get { return GetStr("url") ?? ImageUrl; } }
		public string Picture { get { return GetStr("picture"); } }


		public string Title { get { return GetStr("title"); } }
		public string Description { get { return GetStr("description"); } }
		public string Type { get { return GetStr("type"); } }
		public long? Target { get { return !HasTarget ? null : Fields["target"].AsComposite.GetLong("id") ; } }



		public bool HasTarget { get { return Root.Fields.Has("target"); } }
		public bool HasPicture { get { return Root.Fields.Has("picture"); } }

		public bool HasComments { get { return Root.Fields.Has("comments"); } }
		public CSoftCommentList Comments { get { return GetComments(); } }


		public bool HasWidth { get { return Root.Fields.Has("width") || HasImage && Image.Fields.Has("width"); } }
		public int Width { get { return Fields.Has("width") ? GetInt("width") : (HasImage ? (int)Image.GetLong("width").Value : 0); } }
		public int Height { get { return Fields.Has("height") ? GetInt("height") : (HasImage ? (int)Image.GetLong("height").Value : 0); } }

		public CSoftUser From { get { return !Root.Fields.Has("from") ? null : new CSoftUser(Root.Fields["from"].AsComposite, _cache); } }
	}
}
